using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using MacomberMap.Common.User_Interfaces.Components;
using System.Xml;
using MacomberMap.Common.Types;
using System.Reflection;
using System.Drawing;
using System.ComponentModel;
using MacomberMap.Common.Components;

namespace MacomberMap.Common.User_Interfaces.One_Lines.Elements
{
    /// <summary>
    /// This class holds the information on a descriptor element
    /// </summary>
    public class MM_OneLine_Descriptor : MM_OneLine_Element
    {
        #region Variable declaration
        /// <summary>
        /// The parent element of this descriptor
        /// </summary>
        public MM_OneLine_Element ParentElement;
        #endregion

        #region Initialization
        /// <summary>
        /// Initialize the descriptor
        /// </summary>
        /// <param name="ParentElement">The parent element for this item</param>
        /// <param name="ElemConfig">The configuration for the element</param>
        /// <param name="DisplayParameters">The display parameters for the element</param>        
        public MM_OneLine_Descriptor(MM_OneLine_Element ParentElement, XmlElement ElemConfig, MM_OneLine_Display DisplayParameters)
            : base(DisplayParameters)
        {
            MM_Serializable.ReadXml(ElemConfig, this);
            this.ParentElement = ParentElement;
            this.DisplayParameters = DisplayParameters;            
        }

        /// <summary>
        /// Initialize the descriptor
        /// </summary>
        /// <param name="ParentElement">The parent element for this item</param>
        /// <param name="DisplayParameters">The display parameters for the element</param>
        public MM_OneLine_Descriptor(MM_OneLine_Element ParentElement, MM_OneLine_Display DisplayParameters)
            : base(DisplayParameters)
        {
            this.ForeColor = Color.Gray;
            this.ParentElement = ParentElement;
            this.DisplayParameters = DisplayParameters;    
            if (ParentElement != null)
            this.Location = new Point(ParentElement.Right + 10, ParentElement.Top);
        this.RecomputeSize();
        }

        /// <summary>
        /// Recompute the size of the descriptor based on the text size
        /// </summary>
        public void RecomputeSize()
        {
            try
            {
                using (Graphics g = Graphics.FromHwnd(this.Handle))
                    Size = Size.Ceiling(g.MeasureString(ImageText, Font));
            }
            catch (ObjectDisposedException)
            { }
        }
        #endregion



        #region Description
        /// <summary>
        /// Return a readable string
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (ParentElement == null)
                return "Unattached label";
            else if (ParentElement.BaseElement == null)
                return "Unknown element label";
            else
                return ParentElement.BaseElement.Name + " " + this.Name + " Descriptor (" + this.TEID.ToString() + ")";                 
        }


        #endregion

        /// <summary>
        /// Retrieve the image text for the item
        /// </summary>
        public String ImageText
        {
            get
            {
                if (ParentElement is MM_OneLine_Line && DisplayParameters.Names)
                {
                    MM_Line BaseLine = ParentElement.BaseElement as MM_Line;
                    if (BaseLine == null)
                        return "Unknown line";
                    MM_Substation BaseStation = DisplayParameters.BaseElement as MM_Substation;
                    String Sub1 = String.IsNullOrEmpty(BaseLine.Substation1.LongName) ? BaseLine.Substation1.Name : BaseLine.Substation1.LongName;
                    String Sub2 = String.IsNullOrEmpty(BaseLine.Substation2.LongName) ? BaseLine.Substation2.Name : BaseLine.Substation2.LongName;
                                      
                    if (BaseStation == null)
                        return Sub1 + "\n to " + Sub2 + "\n(" + BaseLine.Name + ")";
                    else if (BaseStation == BaseLine.Substation1 && BaseLine.Substation2 != null)
                        return Sub2 + "\n(" + BaseLine.Name + ")";
                    else if (BaseStation == BaseLine.Substation2 && BaseLine.Substation1 != null)
                        return Sub1 + "\n(" + BaseLine.Name + ")";
                    else
                        return BaseLine.Name;
                }
                else
                    if (ParentElement.BaseElement == null)
                        return this.ToString();
                    else if (ParentElement is MM_OneLine_PricingVector)
                        return (ParentElement as MM_OneLine_PricingVector).EPSMeter.Name + "\n" + (ParentElement as MM_OneLine_PricingVector).RID;                                                                              
                    else if (this is MM_OneLine_SecondaryDescriptor && ParentElement is MM_OneLine_Node)
                        return (ParentElement as MM_OneLine_Node).ResourceNode;
                    else if (this is MM_OneLine_SecondaryDescriptor && ParentElement is MM_OneLine_PokePoint)
                        return (ParentElement as MM_OneLine_PokePoint).BaseNode.ResourceNode;

                    else
                        return ParentElement.BaseElement.Name;
            }
        }


        /// <summary>
        /// Draw the image for the descriptor
        /// </summary>
        /// <param name="g"></param>
        public override void DrawImage(Graphics g)
        {            
            using (SolidBrush br = new SolidBrush(ForeColor))
                if (ParentElement is MM_OneLine_Breaker && (ParentElement as MM_OneLine_Breaker).HasSynchroscope && DisplayParameters.HighlightSynchroscopes)
                    using (Font f = new Font(Font, FontStyle.Bold))
                        g.DrawString(ImageText, f, br, DisplayRectangle, CenterFormat);
                else if (this is MM_OneLine_SecondaryDescriptor)
                    using (Font f = new Font(Font, FontStyle.Italic))
                        g.DrawString(ImageText, f, br, DisplayRectangle, CenterFormat);
                else
                    g.DrawString(ImageText, this.Font, br, DisplayRectangle, CenterFormat);            
        }

    }
}
